﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SharpDepend.Storage
{
    public abstract class IDataFormat
    {
        protected Stream mFile;

        public abstract void Save(CustomNode headNodeToSaveFrom);
        public abstract void Load(CustomNode headNodeToLoadTo);

        public void InitFileStream(Stream filestream)
        {
            filestream.Position = 0;
            mFile = filestream;
        }

        public long Length()
        {
            return mFile.Length;
        }
        public long Position()
        {
            return mFile.Position;
        }
        public void Read(byte[] bytes, int offset, int length)
        {
            mFile.Read(bytes, offset, length);
        }
        public int ReadInt()
        {
            byte[] integer = new byte[4];
            mFile.Read(integer, 0, integer.Length);

//#if(WINDOWS || WINDOWS_PHONE)
            byte t1 = integer[0];
            byte t2 = integer[1];
            integer[0] = integer[3];
            integer[1] = integer[2];
            integer[3] = t1;
            integer[2] = t2;
//#endif

            return System.BitConverter.ToInt32(integer, 0);
        }
        public short ReadShort()
        {
            byte[] short_ = new byte[2];
            mFile.Read(short_, 0, short_.Length);

//#if(WINDOWS || WINDOWS_PHONE)
            byte t1 = short_[0];
            short_[0] = short_[1];
            short_[1] = t1;
//#endif

            return System.BitConverter.ToInt16(short_, 0);
        }
        public byte ReadByte()
        {
            int bigByte = mFile.ReadByte();
            return (byte)bigByte;
        }

        public void Write(byte[] bytes)
        {
            mFile.Write(bytes, 0, bytes.Length);
        }
    }
}
